package org.mybatis.spring.boot.study.dataFlow.importBDRegistrar.config;

import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.filter.AnnotationTypeFilter;

import java.util.Map;

/**
 * @author nicky
 * @date 2021/9/1 下午5:38
 */
public class MyAutoScannerRegistrar implements ImportBeanDefinitionRegistrar {
    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        //不应该这里注册非注册器的bean,不符合spring的生命周期规范

        //扫描注解
        Map<String, Object> annotationAttributes = importingClassMetadata
                .getAnnotationAttributes(TjsBatisMapperScanner.class.getName());
        String[] basePackages = (String[]) annotationAttributes.get("basePackages");

        //扫描类
        ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(registry, false);

        //只扫描@MyMapper
        scanner.addIncludeFilter(new AnnotationTypeFilter(MyMapper.class));
        scanner.scan(basePackages);
    }
}
